package com.loe.logger

import android.app.AlertDialog
import android.content.Context
import android.view.Gravity
import android.view.ViewGroup
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.widget.Toast
import androidx.fragment.app.FragmentActivity
import kotlinx.android.synthetic.main.logger_add_environment_dialog.view.*

/**
 * 添加环境弹框
 *
 * @author zls
 * @since 2025/5/16
 */
class LoggerAddEnvironmentDialog(private val activity: FragmentActivity)
{
    private val dialog: AlertDialog = AlertDialog.Builder(activity).create()
    private lateinit var rootView: ViewGroup

    private var onOk: ((type: String, baseUrl: String) -> Unit)? = null

    fun show(onOk: ((type: String, baseUrl: String) -> Unit))
    {
        this.onOk = onOk
        if (!dialog.isShowing)
        {
            dialog.show()
            val window = dialog.window
            if (window != null)
            {
                window.setContentView(R.layout.logger_add_environment_dialog)
                window.setBackgroundDrawableResource(android.R.color.transparent)
                window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
                window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                window.setGravity(Gravity.CENTER)
                rootView = window.decorView as ViewGroup
                rootView.setPadding(0, 0, 0, 0)
                onShow()
            }
        }
    }

    fun setCancelAfterDismiss()
    {
        dialog.setOnCancelListener()
        {
            it.dismiss()
        }
    }

    private fun onShow()
    {
        setCancelAfterDismiss()
//        dialog.setCanceledOnTouchOutside(false)

        rootView.editBaseUrl.setText(LoeLogger.environmentServeBaseUrl)
        rootView.editBaseUrl.requestFocus()
        rootView.editBaseUrl.setSelection(LoeLogger.environmentServeBaseUrl.findThirdSlashPosition())

        rootView.editBaseUrl.postDelayed({
            val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            imm.showSoftInput(rootView.editBaseUrl, InputMethodManager.SHOW_IMPLICIT)
        }, 100)

        rootView.buttonOk.setOnClickListener()
        {
            val type = rootView.editType.text.toString().trim()
            val baseUrl = rootView.editBaseUrl.text.toString().trim()
            if (type.isBlank())
            {
                Toast.makeText(activity, "请输入环境名称", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            if (baseUrl.isBlank())
            {
                Toast.makeText(activity, "请输入Base URL", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            onOk?.invoke(type, baseUrl)
            dialog.dismiss()
        }

        rootView.buttonCancel.setOnClickListener()
        {
            dialog.dismiss()
        }
    }

    private fun String.findThirdSlashPosition(): Int
    {
        var count = 0
        for ((index, char) in this.withIndex())
        {
            if (char == '/')
            {
                count++
                if (count == 3) return index
            }
        }
        return this.length
    }
}